package com.sunyard.utils.manage;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author zhix.huang
 * @version 1.0
 * @Title DateUtil.java
 * @Package com.sydtech.bms.util
 * @Description 日期工具类
 * @date 2017年11月10日 上午8:50:19
 */

public class DateUtil {

    /**
     * Description
     */
    public final static String DD = "dd";
    /**
     * Description
     */
    public final static String MM = "MM";
    /**
     * Description
     */
    public final static String HHMMSS = "HHmmss";
    /**
     * Description
     */
    public final static String YYYY = "yyyy";
    /**
     * Description
     */
    public final static String YYYYMM = "yyyyMM";
    /**
     * Description
     */
    public final static String YYYYMMDD = "yyyyMMdd";
    /**
     * Description
     */
    public final static String YYYY_MM_DD = "yyyy-MM-dd";
    /**
     * Description
     */
    public final static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
    /**
     * Description
     */
    public final static String YYYYMMDD_HHMMSS = "yyyy-MM-dd HH:mm:ss";
    /**
     * Description
     */
    public final static String HH_MM_SS_SSS = "HH:mm:ss.SSS";
    /**
     * Description
     */
    public final static String HH_MM_SS = "HH:mm:ss";
    /**
     * Description
     */
    public final static String YYYYMMDD_HHMMSSSSS = "yyyy-MM-dd-HH.mm.ss.SSSSSS";

    /**
     * Z  时区（要转换成UTC） 格式如下
     */
    public final static String YYYYMMDD_HHMMSSSSS_Z = "yyyy-MM-dd'T'HH:mm:ss.SSS Z";
    /**
     * Description
     */
    public final static String YYYYMMDD_HHMMSSSSS2 = "yyyy/MM/dd HH:mm:ss";

    /**
     * @param date
     * @return
     * @title getYear
     * @description 获取年份
     * @author Nan Ou
     * @email ou.nan@sunyard.com
     */
    public static String getYear(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(YYYY);
        return sdf.format(date);
    }

    /**
     * @param date
     * @return
     * @title getMonth
     * @description 获取月份
     * @author Nan Ou
     * @email ou.nan@sunyard.com
     */
    public static String getMonth(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(MM);
        return sdf.format(date);
    }

    /**
     * @param date
     * @return
     * @title getDay
     * @description 获取日份
     * @author Nan Ou
     * @email ou.nan@sunyard.com
     */
    public static String getDay(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(DD);
        return sdf.format(date);
    }

    /**
     * 将字符串"yyyy-MM-dd HH:mm:ss"解析成Date
     *
     * @param format
     * @param
     * @return
     * @throws ParseException
     */
    public static Date getDate(String format, String str) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        try {
            return dateFormat.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * 获取yyyyMMddHHmmss格式日期
     *
     * @param str
     * @return Date
     */
    public static Date getDefaultDate(String str) {
        return getDate(YYYYMMDDHHMMSS, str);
    }

    /**
     * 将时间格式化成字符串
     *
     * @param format
     * @param date
     * @return
     */
    public static String getDateStr(String format, Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.format(date);
    }

    /**
     * 获取获取yyyyMMddHHmmss格式格式字符串
     *
     * @param date
     * @return
     */
    public static String getDefaultDateStr(Date date) {
        return getDateStr(YYYYMMDDHHMMSS, date);
    }

    /**
     * 获取当天开始时间
     *
     * @return Date
     */
    public static Date getStartOfTodDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 当天的结束时间
     *
     * @return
     */
    public static Date getEndOfTodDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTime();
    }

    /**
     * 获取给定日期之前或之后若干天
     *
     * @param date 给定日期
     * @param day  偏移天数
     * @return
     */
    public static Date getOffDate(Date date, int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, day);
        return calendar.getTime();
    }

    /**
     * 获取给定日期之前或之后若干月
     *
     * @param date
     * @param month
     * @return
     */
    public static Date getOffMonth(Date date, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, month);
        return calendar.getTime();
    }


    /**
     * 获取给定日期之前或之后若干分钟
     *
     * @param date
     * @param minute
     * @return
     */
    public static Date getOffMinute(Date date, int minute) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, minute);
        return calendar.getTime();
    }

    /**
     * @param beginDate
     * @param endDate
     * @return
     * @Description 根据开始时间和结束时间返回时间段内的时间集合
     */
    public static List<Date> getDatesBetweenTwoDate(Date beginDate, Date endDate) {
        List<Date> lDate = new ArrayList<Date>();
        lDate.add(beginDate);//把开始时间加入集合
        Calendar cal = Calendar.getInstance();
        //使用给定的 Date 设置此 Calendar 的时间
        cal.setTime(beginDate);
        boolean bContinue = true;
        while (bContinue) {
            //根据日历的规则，为给定的日历字段添加或减去指定的时间量
            cal.add(Calendar.DAY_OF_MONTH, 1);
            //测试此日期是否在指定日期之后
            if (endDate.after(cal.getTime())) {
                lDate.add(cal.getTime());
            } else {
                break;
            }
        }
        lDate.add(endDate);//把结束时间加入集合
        return lDate;
    }

    /**
     * Description
     */
    public static boolean isWorkDay(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        if (c.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY ||
                c.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) {
            return false;
        } else {
            return true;
        }
    }


    /**
     * Description
     */
    public static Date getNextDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        date = calendar.getTime();
        return date;
    }


    /**
     * @param smdate
     * @param bdate
     * @return int
     * @throws
     * @Title daysBetween
     * @Description 两个日期相差天数
     */
    public static int daysBetween(Date smdate, Date bdate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        smdate = sdf.parse(sdf.format(smdate));
        bdate = sdf.parse(sdf.format(bdate));
        Calendar cal = Calendar.getInstance();
        cal.setTime(smdate);
        long time1 = cal.getTimeInMillis();
        cal.setTime(bdate);
        long time2 = cal.getTimeInMillis();
        long between_days = (time2 - time1) / (1000 * 3600 * 24);

        return Integer.parseInt(String.valueOf(between_days));
    }

    /**
     * @param smdate
     * @param bdate
     * @return int
     * @throws ParseException
     * @throws
     * @Title monthsBetween
     * @Description 两个月份相差月数
     */
    public static int monthsBetween(Date smdate, Date bdate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        smdate = sdf.parse(sdf.format(smdate));
        bdate = sdf.parse(sdf.format(bdate));
        Calendar cal1 = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();
        cal1.setTime(smdate);
        cal2.setTime(bdate);
        int year = cal2.get(Calendar.YEAR) - cal1.get(Calendar.YEAR);
        return year * 12 + cal2.get(Calendar.MONTH) - cal1.get(Calendar.MONTH);
    }

    //判断传入时间是否是近3个月内
    public static boolean isBetweenMonths(String start_time) {
        StringBuilder sb = new StringBuilder(start_time);
        sb.insert(4, "-");
        Date startTime = DateUtil.getDate("yyyy-MM", sb.toString());
        //获取当前年月
        String month = getDateStr("yyyy-MM", new Date());
        Date nowTime = DateUtil.getDate("yyyy-MM", month);
        Calendar cal1 = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();
        cal1.setTime(startTime);
        cal2.setTime(nowTime);
        //相差几年
        int year = cal2.get(Calendar.YEAR) - cal1.get(Calendar.YEAR);
        //相差几月
        int monthNum = year * 12 + cal2.get(Calendar.MONTH) - cal1.get(Calendar.MONTH);
        if (monthNum >= 0 && monthNum <= 3) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * @param dateStr
     * @return String
     * @throws
     * @Description 将字符日期YYYYMMddHHmmss转化成字符日期yyyy-MM-dd HH:mm:ss
     * @author zhix.huang
     * @date 2017年11月10日 上午8:44:06
     */
    public static String dateformat(String dateStr) {
        if (dateStr == null || "".equals(dateStr) || dateStr.length() != 14) {
            return dateStr;
        }
        //非"yyyyMMddHHmmss"格式的时间转换会报错
        SimpleDateFormat format1 = new SimpleDateFormat("yyyyMMddHHmmss");
        Date date1 = null;
        try {
            //先转化成date
            date1 = format1.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //date转化成string
        SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String str = format2.format(date1);
        return str;
    }

    /**
     * @Description
     * @author zhix.huang
     * @date 2018/3/2 14:30
     */
    public static String mmdd(String st) {
        if (st == null) {
            return "";
        }

        SimpleDateFormat format1 = new SimpleDateFormat("MMdd");
        Date date1 = null;
        try {
            //先转化成date
            date1 = format1.parse(st);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //date转化成string
        SimpleDateFormat format2 = new SimpleDateFormat("MM月dd日");
        String str = format2.format(date1);
        return str;
    }


    /**
     * Description
     */
    public static String TimeStamp2Date(String timestampString, String formats) {
        Long timestamp = Long.parseLong(timestampString) * 1000;
        String date = new SimpleDateFormat(formats, Locale.CHINA).format(new Date(timestamp));
        return date;
    }

    /**
     * 返回下一个月的第一天 格式 yyyy-MM-dd
     *
     * @return
     */
    public static String getFirstDayOfNextMonth() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.MONTH, 1);
        instance.set(Calendar.DAY_OF_MONTH, 1);
        Date time = instance.getTime();
        return simpleDateFormat.format(time);
    }

}
